package cn.windy.samary.module.admin.service.impl;

import cn.windy.samary.module.abstracts.service.impl.AbstractServiceImpl;
import cn.windy.samary.module.admin.entity.Role;
import cn.windy.samary.module.admin.entity.RolePermission;
import cn.windy.samary.module.admin.mapper.RoleMapper;
import cn.windy.samary.module.admin.mapper.RolePermissionMapper;
import cn.windy.samary.module.admin.mapper.UserRoleMapper;
import cn.windy.samary.module.admin.service.IRolePermissionService;
import cn.windy.samary.module.admin.service.IRoleService;
import cn.windy.samary.module.shiro.realm.CustomShiroRealm;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zyl
 * @date 2018-05-26 13:43
 */
@Service
public class RoleServiceImpl extends AbstractServiceImpl<Role> implements IRoleService {

    @Resource
    private CustomShiroRealm customShiroRealm;
    @Resource
    private IRolePermissionService rolePermissionService;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private UserRoleMapper userRoleMapper;
    @Resource
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public int save(Role entity, String menuIds) {
        int i = insertSelective(entity);
        if (StringUtils.isNotBlank(menuIds)) {
            String[] split = menuIds.split(",");
            for (String s : split) {
                RolePermission rolePermission = new RolePermission();
                rolePermission.setRoleId(entity.getId());
                rolePermission.setPermissionId(s);
                rolePermissionService.insertSelective(rolePermission);
            }
        }
        return i;
    }

    @Override
    public int update(Role entity, String menuIds) {
        int i = updateByPrimaryKeySelective(entity);
        RolePermission rp = new RolePermission();
        rp.setRoleId(entity.getId());
        rolePermissionService.delete(rp);
        if (StringUtils.isNotBlank(menuIds)) {
            String[] split = menuIds.split(",");
            for (String s : split) {
                RolePermission rolePermission = new RolePermission();
                rolePermission.setRoleId(entity.getId());
                rolePermission.setPermissionId(s);
                rolePermissionService.insertSelective(rolePermission);
            }
        }

        //清空权限缓存
        customShiroRealm.clearCachedAuthorizationInfo();
        return i;
    }

    @Override
    public int deleteById(String id) {
        int i = deleteByPrimaryKey(id);
        Map<String, Object> map = new HashMap<>();
        map.put("roleId", id);
        userRoleMapper.deleteBy(map);
        rolePermissionMapper.deleteBy(map);
        return i;
    }

    @Override
    public int deleteByIdIn(List<String> ids) {
        int i = roleMapper.deleteByIdIn(ids);
        Map<String, Object> map = new HashMap<>();
        map.put("roleIds", ids);
        userRoleMapper.deleteBy(map);
        rolePermissionMapper.deleteBy(map);
        return i;
    }
}
